package com.controller;

import com.pojo.Address;
import com.pojo.Result;
import com.pojo.SysUser;
import com.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//跨域
@CrossOrigin
@RestController
@RequestMapping("/sysuser")
public class SysUserController {
    //注入SysUserService
    @Autowired
    private SysUserService sysUserService;
    //根据用户名查询用户信息
    @GetMapping("/{username}")
    private Result findUserByUsername(@PathVariable String username){
       SysUser sysUser = sysUserService.findUserByUsername(username);
        Result result = new Result();

        if (sysUser != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(sysUser);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
            //result.setData(list);
        }

        return result;
    }
    //根据用户id查询用户默认地址
    @GetMapping("/address/{id}")
    public Result findUserDefaultAddress(@PathVariable Integer id){
        Address address = sysUserService.findUserDefaultAddress(id);
        Result result = new Result();

        if (address != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(address);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
            //result.setData(list);
        }

        return result;
    }

    //修改昵称
    @PutMapping
    public Result updatename(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.updatename(sysUser);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }

        return result;
    }
    //修改密码
    @PutMapping("/password")
    public Result updatePassword(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.updatepassword(sysUser);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }

        return result;
    }
    //修改头像
    @PutMapping("/headImg")
    public Result updatehead(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.updatehead(sysUser);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }

        return result;
    }

    //查找username是否重名
    @GetMapping("/checkUsername/{username}")
    public Result checkUsername(@PathVariable String username){
        boolean flag = sysUserService.findSameUsername(username);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("用户名可用");
        }else{
            result.setCode(500);
            result.setMsg("用户名不可用");
        }

        return result;
    }


    //注册
    @PostMapping("/regist")
    public Result regist(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.regist(sysUser);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("注册成功");
        }else{
            result.setCode(500);
            result.setMsg("注册失败");
        }

        return result;
    }
}
